<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="media/layui/css/layui.css">
    <script src="media/layui/layui.js"></script>
    <script src="media/js/jquery.min.js"></script>
</head>
<body>
<table class="layui-hide" id="exer" lay-filter="test"></table>
<script type="text/html" id="barDemo">


    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
<script>
    layui.use('table', function(){
        //初始化table组件对象
        var table = layui.table;


        //渲染table
        table.render({
            //z找到table  id叫test的组件
            elem: '#exer'
            ,id:"exerTable"
            ,url:'exer/select.do'


            ,totalRow: true
            //这个很关键，就是解析 json数据库中数据的 一定要注意键值
            ,cols: [[
                {type:'checkbox',fixed: 'left'}
                ,{field:'id', title:'序号', unresize: true, sort: true, totalRowText: ''}
                ,{field:'category', title:'习题种类', edit: 'text'}

                ,{field:'equestion', title:'习题', edit: 'text'}
                ,{field:'eanswer', title:'答案', edit: 'text'}
                ,{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}
            ]]
            ,page: true


        });

        //搜索框有效果的情况
        var  active = {
            reload: function(){
                //执行重载
                table.reload('exerTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }
        };

        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){

                    $.ajax({
                        type: "get",
                        url: "exer/delete.do?id="+ data.id,
                        dataType: "json",
                        success: function (data) {
                            if (data.code == 1) {
                                console.log(data.info)
                                alert(data.info);
                                window.location.href= "exerciseslist.html"
                            } else {
                                layer.msg("删除失败")
                            }
                        }
                    })
                    obj.del();
                });
            }
        });


    });
</script>
</html>
